<?php

namespace app\admin\controller\fastflow\bill;
use app\admin\controller\fastflow\bill\Common;
use think\Db;

/**
 * 项目申报
 *
 * @icon fa fa-circle-o
 */
class Project extends Common
{
    
    /**
     * Project模型对象
     * @var \app\admin\model\fastflow\bill\Project
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fastflow\bill\Project;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("reviewstatusList", $this->model->getReviewstatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->select();

            $list = $this->itemsFilter($list);
            foreach ($list['items'] as $row) {
                $row->visible(['id','name','year','director','mobile','contacts','contacts_mobile','applicant','budget','declaration','abbazzo']);
            }

            $result = array("total" => $list['total'], "rows" => $list['items']);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 选择
     */
    public function select()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->select();

            $list = $this->itemsFilter($list);
            foreach ($list['items'] as $row) {
                $row->visible(['id','name','year','director','applicant','budget','declaration','abbazzo']);
            }

            $result = array("total" => $list['total'], "rows" => $list['items']);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 详情
     * @param null $ids
     * @return mixed
     */
    public function detail($ids = null)
    {
        $row = $this->model
            ->where(['fa_project.id' => $ids])
            ->find();

        $row['declarationfile'] = empty($row['declarationfile'])?'':cdnurl($row['declarationfile'], true);
        $row['abbazzofile'] = empty($row['abbazzofile'])?'':cdnurl($row['abbazzofile'], true);
        $row['finalizefile'] = empty($row['finalizefile'])?'':cdnurl($row['finalizefile'], true);

        //查询所有年度稿
        $modifylist = Db::name('modify')->where(['status' => 2,'project_id' =>$ids])->order('year asc')->field('id,year,annual_draftfile')->select();
        $count = 0;
        if(isset($modifylist[0]) && !empty($modifylist)) {
            foreach ($modifylist as $key => $value) {
                $count++;
                if($count == 1) {
                    $row['annual_draft'] = cdnurl($value['annual_draftfile'], true);
                }
                elseif ($count == 2) {
                    $row['final_draft'] = cdnurl($value['annual_draftfile'], true);
                }
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
